package com.lz.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.lz.form.ItemCategoryDTO;
import com.lz.form.UploadItemDTO;
public class SearchDAO{

public static List getAll(Connection conn){
List list=new ArrayList();
String sql="select * from  category";
PreparedStatement pstmt=null;
ResultSet rs=null;
try{

 pstmt=conn.prepareStatement(sql);


 rs=pstmt.executeQuery();
while(rs.next()){
ItemCategoryDTO dto=new ItemCategoryDTO();
dto.setCategoryid(rs.getString("categoryid"));
dto.setCategoryname(rs.getString("categoryname"));list.add(dto);

}//eof rs.next

rs.close();
pstmt.close();
}
catch(Exception e){
e.printStackTrace();
}
finally{
rs=null;
pstmt=null;
}
return list;

}

public ArrayList getSearchResult(Connection conn, String sql, List queryParams) {
	ArrayList list=new ArrayList();
	
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	try{

	 pstmt=conn.prepareStatement(sql);
	 
	 for(int ctr=0;ctr<queryParams.size();ctr++){
		 pstmt.setString(ctr+1, (String)queryParams.get(ctr));
	 }

	rs=pstmt.executeQuery();
	while(rs.next()){
	
		//image is nothing but a book
	    UploadItemDTO item=new UploadItemDTO();
        item.setImageId(rs.getString("imageid"));
        item.setDescription(rs.getString("description"));
        item.setIsbn(rs.getString("isbn"));
        item.setPrice((rs.getString("price")));
        item.setAuthor(rs.getString("author"));
        item.setPublisher(rs.getString("publisher"));
        item.setYear(rs.getString("year"));
        item.setName(rs.getString("name"));
        item.setEdition(rs.getString("edition"));
		
		list.add(item);

	}//eof rs.next

	rs.close();
	pstmt.close();
	}
	catch(Exception e){
	e.printStackTrace();
	}
	finally{
	rs=null;
	pstmt=null;
	}
	return list;
}

}//eof class